﻿using System;
using System.Web.Mvc;
using CIK.News.Data;
using CIK.News.Domain;
using CIK.News.Framework.Extensions;
using CIK.News.Web.Infrastructure.Security;

namespace CIK.News.Web.Infrastructure.Repository
{
    public class UserRepository : GenericRepository, IUserRepository
    {
        public UserRepository()
            : this(DependencyResolver.Current.GetService<MyDbContext>())
        {
        }

        public UserRepository(MyDbContext context)
            : base(context)
        {
        }

        public User GetUserByUserName(string userName)
        {
            return FindOne<User>(x => x.UserName.Equals(userName, StringComparison.InvariantCulture));
        }

        public bool ValidateUser(string userName, string password)
        {
            var user = GetUserByUserName(userName);

            if (user == null)
                return false;

            return user.UserName.Equals(userName, StringComparison.InvariantCulture)
                   && user.Password.Equals(password, StringComparison.InvariantCulture);
        }

        public UserInfo GetUserInfoByUserName(string userName)
        {
            return GetUserByUserName(userName).MapTo<UserInfo>();
        }
    }
}